import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15
import QtGraphicalEffects 1.15

ApplicationWindow {
    id: root
    width: Screen.width > 1000 ? 1600 : 800
    height: Screen.height > 1000 ? 2560 : 741
    visible: true
    title: "RollingBox"

    // 根据屏幕大小计算缩放因子
    readonly property real scaleFactor: width > 1000 ? 2.0 : 1.0

    // 日期相关属性
    property int currentYear: 1998
    property int currentMonth: 12
    property int currentDay: 21
    property int currentAge: 0
    property string currentConstellation: ""
    
    // 计算最大年份
    readonly property int maxYear: (function() {
        var currentDate = new Date()
        return currentDate.getFullYear() - 18
    })()

    // 星座列表
    property var astroList: ["摩羯座", "水瓶座", "双鱼座", "白羊座", "金牛座", "双子座", 
                              "巨蟹座", "狮子座", "处女座", "天秤座", "天蝎座", "射手座"]
    property var astroDayList: [20, 19, 21, 21, 21, 22, 23, 23, 23, 23, 22, 22]

    Rectangle {
        anchors.fill: parent
        color: "#f0f0f0"

        ColumnLayout {
            anchors.fill: parent
            anchors.margins: 20 * scaleFactor
            spacing: 20 * scaleFactor

            // 标题
            Text {
                text: "年龄&星座获取器"
                font.pixelSize: 24 * scaleFactor
                font.family: "微软雅黑"
                font.bold: true
                color: "#000000"
                Layout.alignment: Qt.AlignHCenter
            }

            // 信息显示区域（放在顶部）
            Rectangle {
                Layout.fillWidth: true
                Layout.preferredHeight: 100 * scaleFactor
                color: "#ffffff"
                radius: 12 * scaleFactor
                
                // 添加阴影效果
                layer.enabled: true
                layer.effect: DropShadow {
                    horizontalOffset: 0
                    verticalOffset: 2 * scaleFactor
                    radius: 8 * scaleFactor
                    samples: 17
                    color: Qt.rgba(0, 0, 0, 0.1)
                }

                Row {
                    anchors.centerIn: parent
                    spacing: 40 * scaleFactor

                    // 年龄显示
                    Row {
                        spacing: 8 * scaleFactor
                        
                        Text {
                            text: "年龄"
                            font.pixelSize: 16 * scaleFactor
                            font.family: "微软雅黑"
                            color: "#999999"
                            anchors.verticalCenter: parent.verticalCenter
                        }
                        
                        Text {
                            text: currentAge + "岁"
                            font.pixelSize: 24 * scaleFactor
                            font.family: "微软雅黑"
                            font.bold: true
                            color: Qt.rgba(1/255, 238/255, 195/255, 1)
                            anchors.verticalCenter: parent.verticalCenter
                        }
                    }

                    // 分隔线
                    Rectangle {
                        width: 1 * scaleFactor
                        height: 40 * scaleFactor
                        anchors.verticalCenter: parent.verticalCenter
                        color: "#e0e0e0"
                    }

                    // 星座显示
                    Row {
                        spacing: 8 * scaleFactor
                        
                        Text {
                            text: "星座"
                            font.pixelSize: 16 * scaleFactor
                            font.family: "微软雅黑"
                            color: "#999999"
                            anchors.verticalCenter: parent.verticalCenter
                        }
                        
                        Text {
                            text: currentConstellation
                            font.pixelSize: 24 * scaleFactor
                            font.family: "微软雅黑"
                            font.bold: true
                            color: Qt.rgba(1/255, 238/255, 195/255, 1)
                            anchors.verticalCenter: parent.verticalCenter
                        }
                    }
                }
            }

            // 中间滚动选择区域（年、月、日）
            RowLayout {
                Layout.fillWidth: true
                Layout.fillHeight: true
                spacing: 0
                Layout.alignment: Qt.AlignHCenter

                // 年份滚动选择器
                Column {
                    Layout.preferredWidth: 200 * scaleFactor
                    Layout.fillHeight: true
                    spacing: 0

                    Text {
                        text: "年"
                        font.pixelSize: 16 * scaleFactor
                        font.family: "微软雅黑"
                        color: "#666666"
                        anchors.horizontalCenter: parent.horizontalCenter
                        height: 30 * scaleFactor
                    }

                    Rectangle {
                        width: parent.width
                        height: parent.height - 30 * scaleFactor
                        color: "transparent"

                        // 选中区域背景
                        Rectangle {
                            id: yearSelectionRect
                            anchors.centerIn: parent
                            width: parent.width
                            height: 40 * scaleFactor
                            color: Qt.rgba(234/255, 234/255, 234/255, 0.06)
                            radius: 0
                        }

                        ListView {
                            id: yearListView
                            anchors.fill: parent
                            clip: true
                            
                            model: maxYear - 1979 + 1
                            delegate: Item {
                                width: yearListView.width
                                height: 40 * scaleFactor
                                
                                property int distance: Math.abs(yearListView.currentIndex - index)
                                
                                Text {
                                    anchors.centerIn: parent
                                    text: (1979 + index) + "年"
                                    font.pixelSize: distance === 0 ? 16 * scaleFactor : 
                                                   (distance === 1 ? 14 * scaleFactor : 
                                                    Math.max(10 * scaleFactor, 16 * scaleFactor - distance * 2))
                                    font.family: "微软雅黑"
                                    color: distance === 0 ? 
                                           Qt.rgba(1/255, 238/255, 195/255, 1) : 
                                           (distance === 1 ? 
                                            Qt.rgba(0, 0, 0, 0.4) : 
                                            Qt.rgba(0, 0, 0, Math.max(0.1, 0.4 - distance * 0.1)))
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                }
                            }
                            
                            snapMode: ListView.SnapToItem
                            highlightRangeMode: ListView.StrictlyEnforceRange
                            preferredHighlightBegin: parent.height / 2 - 20 * scaleFactor
                            preferredHighlightEnd: parent.height / 2 + 20 * scaleFactor
                            highlightMoveDuration: 200
                            
                            Component.onCompleted: {
                                var initialIndex = currentYear - 1979
                                currentIndex = initialIndex
                                positionViewAtIndex(initialIndex, ListView.Center)
                            }
                            
                            onCurrentIndexChanged: {
                                if (currentIndex >= 0) {
                                    currentYear = 1979 + currentIndex
                                    updateDayRange()
                                    updateAge()
                                    updateConstellation()
                                    yearInput.text = currentYear.toString()
                                }
                            }
                        }
                    }
                }

                // 月份滚动选择器
                Column {
                    Layout.preferredWidth: 200 * scaleFactor
                    Layout.fillHeight: true
                    spacing: 0

                    Text {
                        text: "月"
                        font.pixelSize: 16 * scaleFactor
                        font.family: "微软雅黑"
                        color: "#666666"
                        anchors.horizontalCenter: parent.horizontalCenter
                        height: 30 * scaleFactor
                    }

                    Rectangle {
                        width: parent.width
                        height: parent.height - 30 * scaleFactor
                        color: "transparent"

                        // 选中区域背景
                        Rectangle {
                            id: monthSelectionRect
                            anchors.centerIn: parent
                            width: parent.width
                            height: 40 * scaleFactor
                            color: Qt.rgba(234/255, 234/255, 234/255, 0.06)
                            radius: 0
                        }

                        ListView {
                            id: monthListView
                            anchors.fill: parent
                            clip: true
                            
                            model: 12
                            delegate: Item {
                                width: monthListView.width
                                height: 40 * scaleFactor
                                
                                property int distance: Math.abs(monthListView.currentIndex - index)
                                
                                Text {
                                    anchors.centerIn: parent
                                    text: (index + 1) + "月"
                                    font.pixelSize: distance === 0 ? 16 * scaleFactor : 
                                                   (distance === 1 ? 14 * scaleFactor : 
                                                    Math.max(10 * scaleFactor, 16 * scaleFactor - distance * 2))
                                    font.family: "微软雅黑"
                                    color: distance === 0 ? 
                                           Qt.rgba(1/255, 238/255, 195/255, 1) : 
                                           (distance === 1 ? 
                                            Qt.rgba(0, 0, 0, 0.4) : 
                                            Qt.rgba(0, 0, 0, Math.max(0.1, 0.4 - distance * 0.1)))
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                }
                            }
                            
                            snapMode: ListView.SnapToItem
                            highlightRangeMode: ListView.StrictlyEnforceRange
                            preferredHighlightBegin: parent.height / 2 - 20 * scaleFactor
                            preferredHighlightEnd: parent.height / 2 + 20 * scaleFactor
                            highlightMoveDuration: 200
                            
                            Component.onCompleted: {
                                var initialIndex = currentMonth - 1
                                currentIndex = initialIndex
                                positionViewAtIndex(initialIndex, ListView.Center)
                            }
                            
                            onCurrentIndexChanged: {
                                if (currentIndex >= 0) {
                                    currentMonth = currentIndex + 1
                                    updateDayRange()
                                    updateAge()
                                    updateConstellation()
                                    monthInput.text = currentMonth.toString()
                                }
                            }
                        }
                    }
                }

                // 日期滚动选择器
                Column {
                    Layout.preferredWidth: 200 * scaleFactor
                    Layout.fillHeight: true
                    spacing: 0

                    Text {
                        text: "日"
                        font.pixelSize: 16 * scaleFactor
                        font.family: "微软雅黑"
                        color: "#666666"
                        anchors.horizontalCenter: parent.horizontalCenter
                        height: 30 * scaleFactor
                    }

                    Rectangle {
                        width: parent.width
                        height: parent.height - 30 * scaleFactor
                        color: "transparent"

                        // 选中区域背景
                        Rectangle {
                            id: daySelectionRect
                            anchors.centerIn: parent
                            width: parent.width
                            height: 40 * scaleFactor
                            color: Qt.rgba(234/255, 234/255, 234/255, 0.06)
                            radius: 0
                        }

                        ListView {
                            id: dayListView
                            anchors.fill: parent
                            clip: true
                            
                            property int maxDays: getMonthDays(currentYear, currentMonth)
                            
                            model: maxDays
                            delegate: Item {
                                width: dayListView.width
                                height: 40 * scaleFactor
                                
                                property int distance: Math.abs(dayListView.currentIndex - index)
                                
                                Text {
                                    anchors.centerIn: parent
                                    text: (index < 9 ? "0" : "") + (index + 1) + "日"
                                    font.pixelSize: distance === 0 ? 16 * scaleFactor : 
                                                   (distance === 1 ? 14 * scaleFactor : 
                                                    Math.max(10 * scaleFactor, 16 * scaleFactor - distance * 2))
                                    font.family: "微软雅黑"
                                    color: distance === 0 ? 
                                           Qt.rgba(1/255, 238/255, 195/255, 1) : 
                                           (distance === 1 ? 
                                            Qt.rgba(0, 0, 0, 0.4) : 
                                            Qt.rgba(0, 0, 0, Math.max(0.1, 0.4 - distance * 0.1)))
                                    horizontalAlignment: Text.AlignHCenter
                                    verticalAlignment: Text.AlignVCenter
                                }
                            }
                            
                            snapMode: ListView.SnapToItem
                            highlightRangeMode: ListView.StrictlyEnforceRange
                            preferredHighlightBegin: parent.height / 2 - 20 * scaleFactor
                            preferredHighlightEnd: parent.height / 2 + 20 * scaleFactor
                            highlightMoveDuration: 200
                            
                            onMaxDaysChanged: {
                                if (currentDay > maxDays) {
                                    currentDay = maxDays
                                }
                                var newIndex = currentDay - 1
                                if (newIndex >= 0 && newIndex < maxDays) {
                                    currentIndex = newIndex
                                    positionViewAtIndex(newIndex, ListView.Center)
                                }
                            }
                            
                            Component.onCompleted: {
                                var initialIndex = currentDay - 1
                                if (initialIndex >= 0 && initialIndex < maxDays) {
                                    currentIndex = initialIndex
                                    positionViewAtIndex(initialIndex, ListView.Center)
                                }
                            }
                            
                            onCurrentIndexChanged: {
                                if (currentIndex >= 0) {
                                    currentDay = currentIndex + 1
                                    updateAge()
                                    updateConstellation()
                                    dayInput.text = currentDay.toString()
                                }
                            }
                        }
                    }
                }
            }

            // 底部输入区域 - 居中显示
            RowLayout {
                Layout.fillWidth: true
                Layout.preferredHeight: 50 * scaleFactor
                spacing: 10 * scaleFactor
                Layout.alignment: Qt.AlignHCenter

                TextField {
                    id: yearInput
                    Layout.preferredWidth: 80 * scaleFactor
                    Layout.preferredHeight: 32 * scaleFactor
                    placeholderText: "年"
                    text: currentYear.toString()
                    horizontalAlignment: Text.AlignHCenter
                    validator: IntValidator {
                        bottom: 1979
                        top: maxYear
                    }
                    background: Rectangle {
                        color: Qt.rgba(234/255, 234/255, 234/255, 0.15)
                        radius: 16 * scaleFactor
                    }
                    onEditingFinished: {
                        var year = parseInt(text)
                        if (year >= 1979 && year <= maxYear) {
                            currentYear = year
                            yearListView.currentIndex = year - 1979
                            updateDayRange()
                            updateAge()
                            updateConstellation()
                        } else {
                            text = currentYear.toString()
                        }
                    }
                }

                TextField {
                    id: monthInput
                    Layout.preferredWidth: 80 * scaleFactor
                    Layout.preferredHeight: 32 * scaleFactor
                    placeholderText: "月"
                    text: currentMonth.toString()
                    horizontalAlignment: Text.AlignHCenter
                    validator: IntValidator {
                        bottom: 1
                        top: 12
                    }
                    background: Rectangle {
                        color: Qt.rgba(234/255, 234/255, 234/255, 0.15)
                        radius: 16 * scaleFactor
                    }
                    onEditingFinished: {
                        var month = parseInt(text)
                        if (month >= 1 && month <= 12) {
                            currentMonth = month
                            monthListView.currentIndex = month - 1
                            updateDayRange()
                            updateAge()
                            updateConstellation()
                        } else {
                            text = currentMonth.toString()
                        }
                    }
                }

                TextField {
                    id: dayInput
                    Layout.preferredWidth: 80 * scaleFactor
                    Layout.preferredHeight: 32 * scaleFactor
                    placeholderText: "日"
                    text: currentDay.toString()
                    horizontalAlignment: Text.AlignHCenter
                    validator: IntValidator {
                        bottom: 1
                        top: 31
                    }
                    background: Rectangle {
                        color: Qt.rgba(234/255, 234/255, 234/255, 0.15)
                        radius: 16 * scaleFactor
                    }
                    onEditingFinished: {
                        var day = parseInt(text)
                        var maxDays = getMonthDays(currentYear, currentMonth)
                        if (day >= 1 && day <= maxDays) {
                            currentDay = day
                            dayListView.currentIndex = day - 1
                            updateAge()
                            updateConstellation()
                        } else {
                            text = currentDay.toString()
                        }
                    }
                }

                Button {
                    Layout.preferredWidth: 75 * scaleFactor
                    Layout.preferredHeight: 32 * scaleFactor
                    text: "确认"
                    background: Rectangle {
                        color: Qt.rgba(1/255, 238/255, 195/255, 1)
                        radius: 16 * scaleFactor
                    }
                    contentItem: Text {
                        text: parent.text
                        font.pixelSize: 12 * scaleFactor
                        font.family: "微软雅黑"
                        color: "#212426"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        // 优先从滚动选择器获取值，如果没有则从输入框获取
                        var year = yearListView.currentIndex >= 0 ? (1979 + yearListView.currentIndex) : parseInt(yearInput.text)
                        var month = monthListView.currentIndex >= 0 ? (monthListView.currentIndex + 1) : parseInt(monthInput.text)
                        var day = dayListView.currentIndex >= 0 ? (dayListView.currentIndex + 1) : parseInt(dayInput.text)
                        
                        // 验证并更新年份
                        if (year >= 1979 && year <= maxYear) {
                            currentYear = year
                            yearListView.currentIndex = year - 1979
                            yearInput.text = year.toString()
                        }
                        
                        // 验证并更新月份
                        if (month >= 1 && month <= 12) {
                            currentMonth = month
                            monthListView.currentIndex = month - 1
                            monthInput.text = month.toString()
                        }
                        
                        // 验证并更新日期
                        var maxDays = getMonthDays(currentYear, currentMonth)
                        if (day >= 1 && day <= maxDays) {
                            currentDay = day
                            dayListView.currentIndex = day - 1
                            dayInput.text = day.toString()
                        }
                        
                        updateDayRange()
                        updateAge()
                        updateConstellation()
                    }
                }

                Button {
                    Layout.preferredWidth: 75 * scaleFactor
                    Layout.preferredHeight: 32 * scaleFactor
                    text: "关闭"
                    background: Rectangle {
                        color: Qt.rgba(1/255, 238/255, 195/255, 1)
                        radius: 16 * scaleFactor
                    }
                    contentItem: Text {
                        text: parent.text
                        font.pixelSize: 12 * scaleFactor
                        font.family: "微软雅黑"
                        color: "#212426"
                        horizontalAlignment: Text.AlignHCenter
                        verticalAlignment: Text.AlignVCenter
                    }
                    onClicked: {
                        Qt.quit()
                    }
                }
            }
        }
    }

    // 计算月份天数
    function getMonthDays(year, month) {
        var daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
        if (month === 2) {
            // 判断闰年
            if ((year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0)) {
                return 29
            }
            return 28
        }
        return daysInMonth[month - 1]
    }

    // 更新日期范围
    function updateDayRange() {
        var maxDays = getMonthDays(currentYear, currentMonth)
        if (currentDay > maxDays) {
            currentDay = maxDays
        }
    }

    // 计算年龄
    function updateAge() {
        var today = new Date()
        var birthDate = new Date(currentYear, currentMonth - 1, currentDay)
        var age = today.getFullYear() - birthDate.getFullYear()
        var monthDiff = today.getMonth() - birthDate.getMonth()
        if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
            age--
        }
        currentAge = age
    }

    // 更新星座
    function updateConstellation() {
        var month = currentMonth
        var day = currentDay
        var astroIndex = month
        var astroDay = astroDayList[month - 1]
        
        if (day < astroDay) {
            astroIndex = month - 1
            if (astroIndex < 0) astroIndex = 11
        }
        
        currentConstellation = astroList[astroIndex]
    }

    Component.onCompleted: {
        updateDayRange()
        updateAge()
        updateConstellation()
        // 同步输入框显示
        yearInput.text = currentYear.toString()
        monthInput.text = currentMonth.toString()
        dayInput.text = currentDay.toString()
    }
}
